﻿using System;
using System.Collections.Generic;
using Mani.Patient.Business.Domain.Entity;
using NHibernate;
using NHibernate.Expression;

namespace Mani.Patient.Business.Dao {

    public interface IVisitDao : IDao<Visit> {

        IList<Visit> FindByPatient(int pageSize, int pageNumber, Mani.Patient.Business.Domain.Entity.Patient p);
        int GetCountVisit(Mani.Patient.Business.Domain.Entity.Patient p);

    }

    public class VisitDao : DaoBase<Visit>, IVisitDao {

        public IList<Visit> FindByPatient(int pageSize, int pageNumber, Mani.Patient.Business.Domain.Entity.Patient p) {

            ICriteria crit = base.Session.CreateCriteria(typeof(Visit));
            crit.Add(Expression.Eq("Patient", p));
            crit.AddOrder(Order.Desc("VisitDate"));

            return crit.SetMaxResults(pageSize).SetFirstResult(pageSize * pageNumber).List<Visit>();
        }

        public int GetCountVisit(Mani.Patient.Business.Domain.Entity.Patient p)
        {
            ICriteria crit = base.Session.CreateCriteria(typeof(Visit));
            crit.Add(Expression.Eq("Patient", p));

            return crit.SetProjection(Projections.Count("Id")).UniqueResult<int>();
        }
    }
}
